package sicnu.cs.aps.rest;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sicnu.cs.aps.common.ResultInfo;
import sicnu.cs.aps.common.ResultInfoUtil;
import sicnu.cs.aps.domain.entity.ActivityTeam;
import sicnu.cs.aps.domain.entity.ActivityTeamFeedback;
import sicnu.cs.aps.domain.form.ActivityTeamFeedbackForm;
import sicnu.cs.aps.domain.vo.BlogAndTeamVo;
import sicnu.cs.aps.service.IActivitySignService;
import sicnu.cs.aps.service.IActivityTeamFeedbackService;



/**
 * @author CaiKe
 * @date 2022/5/26 14:07
 */
@Slf4j
@RestController
@RequestMapping("/activity/auth")
@RequiredArgsConstructor
@Api(tags = {"活动模块（AUTH）"})
public class ActivityAuthResource {

    private final IActivityTeamFeedbackService teamFeedbackService;
    private final IActivitySignService activitySignService;

    @GetMapping("/blog/list")
    @ApiOperation("活动模块：1-查看博客列表")
    public ResultInfo<PageInfo<ActivityTeamFeedback>> getBlogList(ActivityTeamFeedbackForm activityTeamFeedbackForm){
        /*
        1.查询全部，根据学院、关键字
        2.分页
        3.前缀搜索（‘张%’）
         */
        PageInfo<ActivityTeamFeedback> activityTeamFeedbackList = teamFeedbackService.selectBlogList(activityTeamFeedbackForm);
        return ResultInfoUtil.buildSuccess(activityTeamFeedbackList);
    }

    @GetMapping("/blog/{teamId}")
    @ApiOperation("活动模块：2-根据id查看博客")
    public ResultInfo<BlogAndTeamVo> getBlog(@PathVariable Long teamId){
        if (teamId<=0){
            return ResultInfoUtil.buildError("请输入正确的博客id");
        }
        ActivityTeamFeedback teamFeedback = teamFeedbackService.getById(teamId);
        ActivityTeam activityTeam = activitySignService.getById(teamId);
        if (teamFeedback == null){
            return ResultInfoUtil.buildError("该队伍暂无博客");
        }
        var blogAndTeamToSave = BlogAndTeamVo.builder()
                .teamId(teamFeedback.getTeamId())
                .author(teamFeedback.getAuthor())
                .collegeId(teamFeedback.getCollegeId())
                .blog(teamFeedback.getBlog())
                .blogCreateTime(teamFeedback.getCreateDate())
                .blogLastCreateTime(teamFeedback.getLastEditDate())
                .title(teamFeedback.getTitle())
                .coverPicture(teamFeedback.getCoverPicture())
                .teamName(activityTeam.getName())
                .userId(activityTeam.getUserId())
                .activityId(activityTeam.getActivityId())
                .status(activityTeam.getStatus().getIndex())
                .teamCreateTime(activityTeam.getCreateTime())
                .build();
        return ResultInfoUtil.buildSuccess(blogAndTeamToSave);
    }

}